package com.platform.cdcs.fragment.choose;

import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.TextView;

import com.platform.cdcs.R;
import com.platform.cdcs.fragment.operation.PackageEditFragment;
import com.platform.cdcs.model.ProductList;
import com.platform.cdcs.model.RefershEvent;
import com.platform.cdcs.tool.CacheTool;
import com.platform.cdcs.tool.Constant;
import com.platform.cdcs.tool.FragmentUtil;
import com.platform.cdcs.tool.ViewTool;
import com.sherchen.slidetoggleheader.views.ObservableXListView;
import com.sherchen.slidetoggleheader.views.XListView;
import com.trueway.app.uilib.adapter.EnhancedAdapter;
import com.trueway.app.uilib.fragment.BaseFragment;
import com.trueway.app.uilib.model.ChooseItem;
import com.trueway.app.uilib.tool.Utils;
import com.zhy.http.okhttp.callback.StringCallback;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import de.greenrobot.event.EventBus;
import de.greenrobot.event.Subscribe;
import okhttp3.Call;

/**
 * Created by holytang on 2017/10/6.
 */
public class PackageChooseFragment extends BaseFragment {

    private ObservableXListView listView;
    private ItemAdapter adapter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
        adapter = new ItemAdapter(getContext());
        adapter.addAll(CacheTool.getOutputList(getContext()));
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
        for (int i = 0; i < adapter.getCount(); i++) {
            adapter.getItem(i).setChoose(false);
        }
    }

    @Override
    public void initView(View view) {
        hideThisToolBar(view);
        initLoadImg(view.findViewById(R.id.load));
        getToolBar().setNavigationIcon(R.mipmap.icon_back);
        getToolBar().setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                getActivity().finish();
            }
        });
        setTitle("选择套包产品");
        TextView btn = (TextView) view.findViewById(R.id.button1);
        btn.setVisibility(View.VISIBLE);
        btn.setText("下一步");
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (!adapter.hadChoose()) {
                    Utils.showToast(getContext(), "请先选择!");
                    return;
                }
                Bundle bundle = new Bundle();
                bundle.putString("class", PackageChooseFragment.this.getClass().getName());
                FragmentUtil.navigateToInNewActivity(getActivity(), PackageEditFragment.class, bundle);
            }
        });
        listView = (ObservableXListView) view.findViewById(android.R.id.list);
        listView.setPullRefreshEnable(false);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                i = i - 1;
                adapter.getItem(i).setChoose(!adapter.getItem(i).isChoose());
                adapter.notifyDataSetChanged();
            }
        });
        listView.setAdapter(adapter);
    }

    @Override
    public int layoutId() {
        return R.layout.listview;
    }

    @Subscribe
    public void onEventMainThread(RefershEvent event) {
        if (event.mclass == getClass()) {
            String name = event.bundle.getString("name");
            String unit = event.bundle.getString("unit");
            String qty = event.bundle.getString("qty");
            for (int i = 0; i < adapter.getCount(); i++) {
                ProductList.ProductItem item = adapter.getItem(i);
                if (item.isChoose()) {
                    item.setPackageNum(qty);
                    item.setPackageType(name);
                    item.setPackageUom(unit);
                    CacheTool.changePackage(item);
                }
            }
            CacheTool.saveCache(getContext(), 1);
            adapter.notifyDataSetChanged();
        }
    }

    private class ItemAdapter extends EnhancedAdapter<ProductList.ProductItem> {

        public ItemAdapter(Context context) {
            super(context);
        }

        @Override
        protected void bindView(View paramView, Context paramContext, int position) {
            ProductList.ProductItem item = getItem(position);
            ViewHolder holder = (ViewHolder) paramView.getTag();
            holder.imgView.setSelected(item.isChoose());
            holder.titleView.setText(item.getMaterialNumber());
            holder.textView.setText(item.getMaterialName());
            if (TextUtils.isEmpty(item.getPackageType())) {
                holder.text1View.setVisibility(View.GONE);
            } else {
                holder.text1View.setVisibility(View.VISIBLE);
                holder.text1View.setText(ViewTool.makePackage(getContext(), String.format("%s(%s%s)", item.getPackageType(), item.getPackageNum(), item.getPackageUom())));
            }
            holder.timeView.setText(String.format("%s %s",item.getNowqty(),item.getUom()));

        }

        @Override
        protected View newView(Context context, int position, ViewGroup viewgroup) {
            View rootView = inflater.inflate(R.layout.product_item, viewgroup, false);
            rootView.setBackgroundResource(R.drawable.shape_corner_center);
            ViewHolder holder = new ViewHolder();
            holder.imgView = (ImageView) rootView.findViewById(R.id.img);
            holder.imgView.setVisibility(View.VISIBLE);
            holder.titleView = (TextView) rootView.findViewById(R.id.title);
            holder.timeView = (TextView) rootView.findViewById(R.id.time);
            holder.textView = (TextView) rootView.findViewById(R.id.text);
            holder.text1View = (TextView) rootView.findViewById(R.id.text1);
            rootView.setTag(holder);
            return rootView;
        }

        public boolean hadChoose() {
            for (ProductList.ProductItem item : dataList) {
                if (item.isChoose()) {
                    return true;
                }
            }
            return false;
        }

        public String getItemCode() {
            for (ProductList.ProductItem item : dataList) {
                if (item.isChoose()) {
                    return item.getMaterialNumber();
                }
            }
            return "";
        }
    }

    private class ViewHolder {
        TextView titleView, timeView, textView, text1View;
        ImageView imgView;
    }
}
